package com.scope.activities;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.scope.R;
import com.scope.customViews.ImagePager;
import com.scope.imageFilters.Adaptive;
import com.scope.imageFilters.AdaptiveType;
import com.scope.imageFilters.EdgeDetection;
import com.scope.imageFilters.FilterManager;
import com.scope.imageFilters.GrayScale;
import com.scope.imageFilters.Morphing;
import com.scope.imageFilters.MorphoParam;
import com.scope.imageFilters.ThreshParam;
import com.scope.imageFilters.Threshold;
import com.scope.imageTextReader.PreProcessAsync;

public class PreprocessActivity extends CustomActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	mFilterName = new ArrayList<String>();
        mImagePath = getIntent().getStringExtra(SELECTED_PATH);
        setContentView(R.layout.activity_preprocess);
        super.onCreate(savedInstanceState);
        applyFilters();
    }

    @Override
    protected void initializeViews(Bundle savedInstanceState) {
        mTextView = (TextView)findViewById(R.id.filterName);
        mImageLayout = (LinearLayout)findViewById(R.id.filteredImage);
        mProgressBar = (ProgressBar)findViewById(R.id.filterProgress);
        
        mImagePager = new ImagePager(this);
        mImageLayout.addView(
        		mImagePager,
        		new LinearLayout.LayoutParams(
        				LinearLayout.LayoutParams.MATCH_PARENT,
        				LinearLayout.LayoutParams.MATCH_PARENT)
        );
        mImagePager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageSelected(int idx) {
				if (mImagePager.getPagingEnabled()) {
					if (idx < 2) {
						mTextView.setText(FILTERS_APPLIED + " (orginal image)");
					} else if (idx <= mFilterName.size()){
						mTextView.setText(FILTERS_APPLIED + " (" + mFilterName.get(idx - 1) + ")");
					} else {
						mTextView.setText(FILTERS_APPLIED);
					}
				}
			}
        });
    }

    @Override
    protected void positionViews() {
    }

    private void applyFilters() {
    	mTextView.setText(IMAGE_READING);
    	mProgressBar.setVisibility(View.VISIBLE);
    	mImagePager.setPagingEnabled(false);
    	mPreProcessDone = false;
        mPreProcessAsync = new PreProcessAsync(mImagePath, this);
        mPreProcessAsync.execute(getFilterMangers());
    }

    private FilterManager[] getFilterMangers() {
        FilterManager[] filters = new FilterManager[] {
        		new FilterManager (
        				getApplicationContext(),
        				EdgeDetection.getFilterName(),
        				""),
		        new FilterManager(
		        		getApplicationContext(),
		        		GrayScale.getFilterName(),
		        		""),
		        new FilterManager(
		        		getApplicationContext(),
		        		Threshold.getFilterName(),
		        		new ThreshParam(150, 255, ThreshParam.ThreshType.BINARY).toString()),
		        new FilterManager(
		        		getApplicationContext(),
		        		Adaptive.getFilterName(),
		        		AdaptiveType.GAUSSIAN.toString()),
		        new FilterManager(
		        		getApplicationContext(),
		        		Morphing.getFilterName(),
		        		new MorphoParam(30, 10).toString())
		        };
        return filters;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.preprocess, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void asyncProgress(int idx, Bitmap bitmap, String filterName) {
    	try {
    		mImagePager.addImage(bitmap);
    	} catch (Exception e){
    		e.printStackTrace();
    	}
    	
    	mFilterName.add(filterName);
    	if (filterName != null && filterName.length() > 0) {
    		mTextView.setText(APPLYING + filterName + FILTERING);
    	}
    }
    
    public void asyncEnd(Bitmap bitmap) {
    	try {
    		mImagePager.addImage(bitmap);
    	} catch (Exception e){
    		e.printStackTrace();
    	}
    	
    	mTextView.setText(FILTERS_APPLIED);
    	mProgressBar.setVisibility(View.GONE);
    	mImagePager.setPagingEnabled(true);
    	mPreProcessDone = true;
    }

    private List<String> mFilterName;
    private boolean mPreProcessDone;
    private String mImagePath;
    private PreProcessAsync mPreProcessAsync;
    private TextView mTextView;
    private LinearLayout mImageLayout;
    private ImagePager mImagePager;
    private ProgressBar mProgressBar;
    
    @Override
    public void setAccent(int accent) {
    }

    public static final String IMAGE_READING = "reading image...";
    public static final String APPLYING = "applying (";
    public static final String FILTERING = ") filter to image...";
    public static final String FILTERS_APPLIED = "preprocessing done...";
}
